﻿using System.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Web.UI.WebControls.WebParts;

namespace Marbles.Core.Web.Controls.WebParts
{
    [ToolboxData("<{0}:WorldClock runat=server></{0}:WorldClock>")]
    public class WorldClock : WebPart
    {

        [Category("Behaviour")]
        public int AutoRefreshInterval
        {
            get
            {
                object obj = ViewState["AutoRefreshInterval"];
                if (obj == null) { return 0; }
                return Convert.ToInt32(obj);
            }
            set { ViewState["AutoRefreshInterval"] = value; }
        }

        [Category("Behaviour")]
        public bool AutoRefresh
        {
            get
            {
                object obj = ViewState["AutoRefresh"];
                if (obj == null) { return false; }
                return Convert.ToBoolean(obj);
            }
            set
            { ViewState["AutoRefresh"] = value; }

        }


        [Category("Appearance")]
        public string WorldClockUrl
        {
            get
            {
                object obj = ViewState["WorldClockUrl"];
                if (obj == null) { return DefaultWorldClockUrl; }
                return obj.ToString();
            }
            set
            {
                ViewState["WorldClockUrl"] = value;
            }
        }

        [Category("Appearance")]
        public DateTime Value
        {
            get
            {
                object obj = ViewState["Value"];
                if (obj == null) { return DateTime.Now; }
                return Convert.ToDateTime(obj);
            }
            set { ViewState["Value"] = value; }
        }


        private const string DefaultWorldClockUrl = "http://www.timeanddate.com/worldclock/showsunmap.php?iso={0}";

        protected override void CreateChildControls()
        {
            Image img = new Image();
            img.ID = this.ClientID + "_Image";
            img.Width = this.Width;
            img.Height = this.Height;
            img.AlternateText = this.ToolTip;
            img.ToolTip = this.ToolTip;
            img.ImageUrl = GetUrl(this.WorldClockUrl);

            this.Controls.Add(img);

            if (AutoRefresh)
            {
                this.Controls.Add(CreateRefreshScript(img.ClientID));
            }

        }

        private LiteralControl CreateRefreshScript(string ImageId)
        {
            LiteralControl lt = new LiteralControl();
            StringBuilder sb = new StringBuilder();
            lt.ID = this.ClientID + "_Script";

            sb.Append("<script language=\"javascript\">");
            sb.Append(string.Format("function {0}_Refresh()", this.ClientID));
            sb.Append("{");
            sb.Append("var dte = new Date();");
            sb.AppendFormat("document.getElementById('{0}').src = '{1}&' + dte.getTime();", ImageId, GetUrl(this.WorldClockUrl));
            sb.Append("}");
            sb.Append(string.Format("self.setInterval('{0}_Refresh()', {1});", this.ClientID, (AutoRefreshInterval * 1000)));
            sb.Append("</script>");

            lt.Text = sb.ToString();
            return lt;
        }

        protected virtual string GetUrl(string value)
        {
            string url = value.Trim();
            string datevalue = string.Format("{0}T{1}", Value.ToString("yyyyMMdd"), Value.ToString("HHmmss"));
            url = string.Format(value.Trim(), datevalue);
            return url;
        }
    }
}
